package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.model.InsCollectTask;
import com.iwhalecloud.uncc.utils.BatchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @author zq
 * @date 2021/10/12 10:55
 * @description
 */
@Slf4j
@Repository("insCollectTaskArchiverDao")
public class InsCollectTaskArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `TASK_CODE`, `STRATEGY_ID`, `TEMPLATE_ID`, `SOURCE_ID`, `PROCESS_ID`, `ABSTRACT_ID`, `BATCH_NO`, `SERVICE_ID`, `DISPATCH_DATE`, `START_DATE`, `END_DATE`, `STATE`, `ERROR_DESC`, `POLICY_TYPE`, `IS_SUPPLEMENT`, `PARENT_TASK_ID`, `ROOT_TASK_ID`, `PARTITION_CODE`");
        sql.append(" FROM INS_COLLECT_TASK");
        sql.append(" WHERE END_DATE BETWEEN ? AND ?");
        sql.append(" AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        sql.append(" LIMIT "+ aP.getNumber());
        List<InsCollectTask> list = jdbcTemplate.query(sql.toString(),
                new Object[]{aP.getStartDate(),aP.getEndDate()},
                new BeanPropertyRowMapper<>(InsCollectTask.class));
        if(!list.isEmpty()){
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        return null;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_collect_task`");
        sql.append(" (`ID`, `TASK_CODE`, `STRATEGY_ID`, `TEMPLATE_ID`, `SOURCE_ID`, `PROCESS_ID`, `ABSTRACT_ID`, `BATCH_NO`, `SERVICE_ID`, `DISPATCH_DATE`, `START_DATE`, `END_DATE`, `STATE`, `ERROR_DESC`, `POLICY_TYPE`, `IS_SUPPLEMENT`, `PARENT_TASK_ID`, `ROOT_TASK_ID`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:taskCode,:strategyId,:templateId,:sourceId,:processId,:abstractId,:batchNo,:serviceId,:dispatchDate,:startDate,:endDate,:state,:errorDesc,:policyType,:isSupplement,:parentTaskId,:rootTaskId,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_COLLECT_TASK SET ARCHIVER_FLAG = " + flag + " WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_COLLECT_TASK WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
